package cn.edu.xjtu.nbrash.xbarsuite.result;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import cn.edu.xjtu.nbrash.xbarsuite.R;
import cn.edu.xjtu.nbrash.xbarsuite.RashIntents;

import com.google.zxing.Result;

/**
 * RashResultActivity.java
 *
 * @author Jingkai Tang jingkaitang@gmail.com
 * @version 0.0.1 Nov 17, 2014 2:17:42 PM
 *
 */
public class RashResultActivity extends Activity {

	private ListView infoItems;
	private LinearLayout resultAction;
	private ImageView typeImg;
	private TextView typeName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rash_result);

		typeImg = (ImageView) findViewById(R.id.result_type_img);
		typeName = (TextView) findViewById(R.id.result_type_name);
		infoItems = (ListView) findViewById(R.id.result_info_items);
		resultAction = (LinearLayout) findViewById(R.id.result_action);

		Intent intent = getIntent();
		String strResult = intent
				.getStringExtra(RashIntents.Result.EXTRA_Result);
		Result result = new Result(strResult, null, null, null);

		final ResultHandler resultHandler = ResultHandlerFactory
				.makeResultHandler(this, result);
		typeImg.setImageResource(resultHandler.getIcon());
		typeName.setText(resultHandler.getDisplayTitle());

		infoItems.setAdapter(new SimpleAdapter(this, resultHandler.getData(),
				R.layout.result_info_item, new String[] { "key", "value" },
				new int[] { R.id.item_key, R.id.item_value }));

		Button[] buttons = new Button[resultHandler.getButtonCount()];
		for (int i = 0; i < buttons.length; i++) {
			FrameLayout gap = new FrameLayout(this);
			LayoutParams gapLayoutParams = new LayoutParams(
					LayoutParams.MATCH_PARENT, 10);
			gap.setLayoutParams(gapLayoutParams);
			resultAction.addView(gap);

			buttons[i] = new Button(this);
			LayoutParams layoutParams = new LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
			buttons[i].setLayoutParams(layoutParams);
			buttons[i].setText(resultHandler.getButtonText(i));
			buttons[i].setBackgroundResource(R.drawable.rounded_rect_btn);
			final int index = i;
			buttons[i].setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					resultHandler.handleButtonPress(index);
				}
			});
			resultAction.addView(buttons[i]);
		}

	}
}
